/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mapeServer;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 *
 * @author carmelo
 */
public class RealConnector implements InternalConnector {
    
    @Resource(lookup = "jms/InternalConnectionFactory")
    private static ConnectionFactory connectionFactory;
    @Resource(lookup = "jms/InternalQueue")
    private static Queue queue;
        
    @Override
    public void send(String payloadmessage) {
        javax.jms.Connection connection = null;
        try {
            connection = connectionFactory.createConnection();
            
            Session session = connection.createSession(
                    false,
                    Session.AUTO_ACKNOWLEDGE);  
            MessageProducer producer = session.createProducer(queue);
            TextMessage message = session.createTextMessage();
            message.setText(payloadmessage);
            System.out.println("Sending message: " + message.getText());
            producer.send(message);

        } catch (JMSException e) {
            System.err.println("Exception occurred: " + e.toString());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }
}
